package com.hyt.it.ogt.kq.service.bm.mq;

import com.hyt.it.ogt.kq.service.bm.service.IProjectService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 报名项目数据统计消息消费者
 *
 * @author huangwu
 * @date 2022-09-05
 */
@Component
@Slf4j
public class ProjectStatisticConsumer {

    @Resource
    private IProjectService projectService;

    @RabbitListener(
            bindings = {@QueueBinding(value = @Queue(value = "${mq.projectStatistic.queue}",
                    autoDelete = "false", durable = "true"),
                    exchange = @Exchange(value = "${mq.direct}",
                            type = ExchangeTypes.DIRECT),
                    key = {"${mq.projectStatistic.route}"})},
            concurrency = "${mq.projectStatistic.concurrency}")
    public void projectStatisticListener(String projectId, Message message, Channel channel) throws IOException {
        log.debug("[报名统计]消费报名数据统计:【{}】", projectId);
        try {
            projectService.statisticProject(projectId);
        } catch (Exception e) {
            log.error("[报名统计]消费报名数据统计异常【{}】", projectId, e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
